package com.lzz.bottomencapsulation.manager;

import android.content.Context;
import android.util.Log;

import com.lzz.bottomencapsulation.utils.LUtils;

import net.security.device.api.SecurityCode;
import net.security.device.api.SecurityDevice;
import net.security.device.api.SecurityInitListener;
import net.security.device.api.SecurityToken;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lhx
 * @description:  阿里云风险识别管理类
 * @date : 2024/1/23 14:05
 */
public class ALiFeiXianShiBieManager {
    static String userAppKey="shb311d5achscb169f6409705ca43828";

    private static ALiFeiXianShiBieManager instance;

    private ALiFeiXianShiBieManager() {
    }

    public static ALiFeiXianShiBieManager getInstance() {
        if (instance == null) {
            synchronized (ALiFeiXianShiBieManager.class) {
                if (instance == null) {
                    instance = new ALiFeiXianShiBieManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     * @param context
     */
    public void init(Context context){
        Map<String, String> options = new HashMap<>();
//      options.put("CustomUrl","http://jhtest.heyilianchuangkeji.top:8090/ali/device/callback");//服务器接口地址
//      options.put("CustomHost","jhtest.heyilianchuangkeji.top:8090");//域名
//      options.put("DataType","NO_UNIQUE_DEVICE_DATA|NO_IDENTIFY_DEVICE_DATA|NO_BASIC_DEVICE_DATA|NO_EXTRA_DEVICE_DATA");//获取参数
        SecurityDevice.getInstance().initWithOptions(context, userAppKey, options, new SecurityInitListener() {
            @Override
            public void onInitFinish(int i) {
                LUtils.e("AliyunDevice",i);
            }
        });
    }

    /**
     * 获取token
     */
    public void getToken(){
        new Thread() {
            @Override
            public void run() {
                SecurityToken st = SecurityDevice.getInstance().getDeviceToken();
                if(null != st){
                    if(SecurityCode.SC_SUCCESS == st.code){
                        Log.d("AliyunDevice", "token: " + st.token);

                        // 发送token到业务自有服务器并查询阿里云设备风险识别接口。
                        // sendToAPPServer(st.token);
                    } else {
                        Log.e("AliyunDevice", "getDeviceToken error, code: " + st.code);
                    }
                } else {
                    Log.e("AliyunDevice", "getDeviceToken is null.");
                }
            }
        }.start();
    }

}